package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Bundle;
import android.os.StatFs;
import android.text.TextUtils;
import android.text.format.Formatter;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityHardwareInfoBinding;
import stark.common.basic.device.DeviceInfoUtil;
import stark.common.basic.device.StorageUtil;
import stark.common.basic.event.EventStatProxy;

public class HardwareInfoActivity extends BaseAc<ActivityHardwareInfoBinding> {




    @Override
    protected int onCreate() {
        return R.layout.activity_hardware_info;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivHardwareInfoBack.setOnClickListener(v -> onBackPressed());
    }

    @Override
    protected void initData() {
        mDataBinding.tvBasicInfo.setText(DeviceInfoUtil.getDeviceModel());//型
        mDataBinding.tvCpu.setText(getCpu());//cpu
        mDataBinding.tvMemory.setText(getRAMTotalMemorySize(mContext));//内存
        StatFs intStaFs = StorageUtil.getExternalMemory();
        mDataBinding.tvStorage.setText(ConvertUtils.byte2FitMemorySize(intStaFs.getTotalBytes(), 0));//存储
        mDataBinding.tvResolution.setText(ScreenUtils.getScreenHeight() + "*" + ScreenUtils.getScreenWidth() + getString(R.string.pixel_text));//分辨率
    }


    //获取CPU
    public static String getCpu() {
        String str1 = "/proc/cpuinfo";
        String str2 = "";
        String cpuName = "";

        try {
            FileReader fileReader = new FileReader(str1);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while ((str2 = bufferedReader.readLine()) != null) {
                if (TextUtils.isEmpty(str2)) {
                    continue;
                }
                String[] arrayOfString = str2.split(":\\s+", 2);
                if (TextUtils.equals(arrayOfString[0].trim(), "Hardware")) {
                    cpuName = arrayOfString[1];
                    break;
                }
            }

            bufferedReader.close();
            fileReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cpuName;
    }

    public static String getRAMTotalMemorySize(final Context context) {
        //获得ActivityManager服务的对象
        ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        //获得MemoryInfo对象
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        //获得系统可用内存，保存在MemoryInfo对象上
        mActivityManager.getMemoryInfo(memoryInfo);
        long memSize = memoryInfo.totalMem;
        //字符类型转换
        String availMemStr = formateFileSize(context, memSize);
        return availMemStr;

    }

    //调用系统函数，字符串转换 long -String KB/MB
    public static String formateFileSize(Context context, long size) {
        return Formatter.formatFileSize(context, size);
    }

}